import request from 'request-promise'

function Request(){}

Request.prototype.constructor = Request;

/**
 * 发送请求
 * @param options
 * @returns {*}
 */
Request.prototype.transfer = function  (options){
    let param = {};
    if( options.headers ){
        param = {
            uri: options.uri,
            method: options.method,
            form: options.params,
            formData: options.formData,
            body: options.body,
            json: true,
            headers:options.headers
        };
    }else{
        param = {
            uri: options.uri,
            method: options.method,
            form: options.params,
            formData: options.formData,
            body: options.body,
            json: true
        };
    }
    
    return request(param);
}

Request.prototype.transferFile = function  (options){
    let param = {
        uri: options.uri,
        method: options.method,
        formData: options.formData,
        headers:{
            'transfer-encoding':'chunked'
        },
        json: true
    };
    return new Promise((resolve, reject) => {
        req(param,function (error, response, body) {
            if (error) {
                reject(error);
            }
            resolve(body);
        });
    });
}

export default new Request;